package com.yloveg.common;

import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;
import com.yloveg.entity.VPay;

/**
        * 获取微信APPID和secret工具类
        */
 
public  class AccessTokenUtil {

	/**
	 * 获取小程序 access_token ,每天最多获取200次，一次2小时失效
	 * https://developers.weixin.qq.com/miniprogram/dev/api/open-api/access-token/getAccessToken.htm
	 */
	public synchronized static String getAccessTockenResult() {
		RestTemplate restTemplate = new RestTemplate();
		ResponseEntity<String> responseEntity = null;
		//老公司
		String appid = "wx58fb0633fc3c311c";         
		String secret = "e316ad909524f73d1049825ec8044c27";
		
        //String appid = "wxf882faddca97fa23";         
        // String secret = "1acbda88880304fef234c31394ed3fe0";

		// 微信的接口
		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret;
		// 进行网络请求,访问url接口
		responseEntity = restTemplate.exchange(url, HttpMethod.GET, null, String.class);
		System.out.println("进行网络请求获取小程序ACCESS_TOKEN成功返回数据:" + responseEntity);
		// errcode 的合法值0
		// 根据返回值进行后续操作
		if (responseEntity != null && responseEntity.getStatusCode() == HttpStatus.OK) {
			String sessionData = responseEntity.getBody();
			System.out.println("远程获取小程序ACCESS_TOKEN成功返回数据:" + sessionData);
			JSONObject JS = JSONObject.parseObject(sessionData);
			return JS.getString("access_token");
		}else{
			return null;
		}
 
	}
	
	public static void main(String[] args) {
		System.out.println("========"+getAccessTockenResult());
	}

}
